/**
 * FlightSystem.java
 * Dec 28, 2009
 */
package com.dlplay.flightinfo.core;

import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.dlplay.flightinfo.client.NotificationManager;
import com.dlplay.flightinfo.client.SimpleNotificationMngr;
import com.dlplay.flightinfo.data.AirFlightCode;
import com.dlplay.flightinfo.data.AirlineCode;
import com.dlplay.flightinfo.data.Airport;
import com.dlplay.flightinfo.data.AirportTerminalCode;
import com.dlplay.flightinfo.data.FlightInfo;
import com.dlplay.flightinfo.data.RealTimeFlightInfo;
import com.dlplay.flightinfo.data.Weather;
import com.dlplay.flightinfo.db.DumpFlightInfoDbMngr;
import com.dlplay.flightinfo.db.FlightInfoDbJDOImpl;
import com.dlplay.flightinfo.db.FlightInfoDbMngr;
import com.dlplay.flightinfo.db.FlightInfoDbMngrImpl;
import com.dlplay.flightinfo.source.DataSourceManager;

/**
 * @author zhao yu
 *
 */
public class FlightSystem extends HttpServlet {	
	private static final long serialVersionUID = -8510774051959853417L;
	private static final Logger log = Logger.getLogger(FlightSystem.class.getName());
	private static DataSourceManager dataSourceManager;
	private static FlightManager flightManager;
	private static AirportManager airportManager;
	private static FlightInfoDbMngr flightInfoDbManager;
	private static NotificationManager notificationManager;
	private static PeriodManager periodManager;
	
	
	/**
	 * @return the airportManager
	 */
	public static AirportManager getAirportManager() {
		return airportManager;
	}

	/**
	 * @return the flightManager
	 */
	public static FlightManager getFlightManager() {
		return flightManager;
	}
	
	/**
	 * @return the DataSourceManager
	 */
	public static DataSourceManager getDataSourceManager() {
		return dataSourceManager;
	}
	
	/**
	 * @return the FlightInfoDbManager
	 */
	public static FlightInfoDbMngr getFlightInfoDbManager() {
		return flightInfoDbManager;
	}
	
	

	/**
	 * @return the periodManager
	 */
	public static PeriodManager getPeriodManager() {
		return periodManager;
	}

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	@Override
	public void init() throws ServletException {
		log.info("FlightSystem init");
		dataSourceManager = new DataSourceManager();
		flightManager = new SimpleFlightManager();
		airportManager = new SimpleAirportManager();
		//flightInfoDbManager = new FlightInfoDbMngrImpl();
		flightInfoDbManager = new FlightInfoDbJDOImpl();
		notificationManager = new SimpleNotificationMngr();
		periodManager = new PeriodManager();
		dataSourceManager.Init();
		//InitDebugData();
		super.init();
	}
	
	/*
	 * Init Data for Debuging, should be disable when release
	 */
	private void InitDebugData() {
		log.info( "InitDebugData");		
		
		RealTimeFlightInfo f = flightManager.getRealTimeFlightInfo(new AirFlightCode(AirlineCode.CA, 1856));		
		
//		flightManager.addFlight(new Flight("CA1858"));
		f = flightManager.getRealTimeFlightInfo(new AirFlightCode(AirlineCode.CA, 1686));
		if (f == null)
		{
			log.severe("Flight CA1585 not found!");
		}
//		airportManager.addAirport(new Airport("Beijing", "PEK"));
		Weather weather = new Weather();
		weather.setWeather("Clear");
		Airport airport = new Airport(AirportTerminalCode.PVG, weather);
		airport.getWeather().setTemperatureHigh(6);
		airportManager.updateAirport(airport);
				
		weather = new Weather();
		weather.setWeather("晴朗");
		airport = new Airport(AirportTerminalCode.PEK_T3, weather);
		airport.getWeather().setTemperatureHigh(10);
		airportManager.updateAirport(airport);		
	
	}

	public static NotificationManager getNotificationManager() {
		return notificationManager;		
	}
}
